<?php
defined('_JEXEC') or die();

class WrdControllerCemail extends JControllerLegacy 
{
	
	protected $option;
	protected $controller;
	
	function __construct( $default = array()){
		$this->option = 'com_wrd';
		$this->controller = 'cemail';
		
		parent::__construct( $default );
	}

	function display()
	{
		switch($this->getTask()){
			case 'add':{
				JRequest::setVar( 'hidemainmenu', 1 );
				JRequest::setVar( 'layout', 'form'  );
				JRequest::setVar( 'view'  , 'cemail');
			} 
			break;
			default:
				JRequest::setVar( 'view'  , 'cemail');
				break;
		}
		parent::display();
	}
        
      //save profile info
    function save(){ 
       $post=JRequest::get('post', JREQUEST_ALLOWHTML);
       if($post['action'] != 'pickup_notice'){
			$post['template_d1'] = '' ;
			$post['template_d1_vi'] = '' ;
       }
       $params = JComponentHelper::getParams('com_wrd');
       $model=$this->getModel('cemail');
	   //get date current
      // echo "<pre>";print_r($post);exit;
       $id_category=$model->store($post,'cemail');
       if($id_category!=0){
			$this->setRedirect('index.php?option='.$this->option.'&controller='.$this->controller,'Saved');
			return true;                
       }
       else{
            $this->setRedirect('index.php?option='.$this->option.'&controller='.$this->controller,'Error Saved');
            return false;
       }
    }    
   
    function remove()
	{
		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );

		$cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
		JArrayHelper::toInteger($cid);

		if (count( $cid ) < 1) {
			JError::raiseError(500, JText::_( 'Select an item to delete' ) );
		}

		$model = $this->getModel('cemail');
		if(!$model->delete($cid)) {
			echo "<script> alert('".$model->getError(true)."'); window.history.go(-1); </script>\n";
		}
		
		$this->setRedirect( 'index.php?option='.$this->option.'&controller='.$this->controller,'Removed');
	}
   
    function publish()
	{
		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );

		$cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
		JArrayHelper::toInteger($cid);

		if (count( $cid ) < 1) {
			JError::raiseError(500, JText::_( '<p style="color:red">Please select an item to publish</p>' ) );
		}

		$model = $this->getModel('cemail');
		if(!$model->publish($cid, 1)) {
			echo "<script> alert('".$model->getError(true)."'); window.history.go(-1); </script>\n";
		}
		$this->setRedirect('index.php?option='.$this->option.'&controller='.$this->controller,'Published');
	}

	function unpublish(){
		
		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );

		$cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
		JArrayHelper::toInteger($cid);

		if (count( $cid ) < 1) {
			JError::raiseError(500, JText::_( '<p style="color:red">Please select an item to publish</p>'  ) );
		}

		$model = $this->getModel('cemail');
		
		if(!$model->publish($cid, 0)) {
			echo "<script> alert('".$model->getError(true)."'); window.history.go(-1); </script>\n";
		}
		
		$this->setRedirect('index.php?option='.$this->option.'&controller='.$this->controller,'Unpublished');
	}
}